<template>
  <div class="login_box">
    <div class="avatar_box">
      <!--头像-->
      <img :src="this.src" alt/>
    </div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input type="username" v-model="ruleForm.username" prefix-icon="iconfont el-icon-s-custom"
                  autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" prefix-icon="iconfont el-icon-key"
                  autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item class="btns" style="margin-bottom: 0px">
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>

      <el-link type="primary" style="float:right;height: 40px;margin-right: 10px" @click="registered">注册账号</el-link>
      <el-link type="primary" style="float:right;height: 40px;margin-right: 10px">|</el-link>
      <el-link type="primary" style="float:right;height: 40px;margin-right: 10px" @click="retrieve">忘记密码</el-link>
    </el-form>
  </div>
</template>


<script>
export default {
  data() {
    let validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error("请输入用户名/手机号/邮箱"));
      } else {
        try {
          this.src = require('D:/IDEA/SpringBoot+Vue/img/' + this.ruleForm.username + '.jpg')
        } catch (e) {
          this.src = require('../assets/img/default.png')
        }
        callback();
      }
    };
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (value.length < 5 || value.length > 19) {
          callback(new Error('请输入6-18位的密码'));
        } else {
          callback();
        }
      }
    };

    return {
      ruleForm: {
        username: '',
        password: ''
      },
      src: require('../assets/img/default.png'),
      rules: {
        username: [
          {validator: validateName, trigger: 'blur'}
        ],
        password: [
          {validator: validatePass, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(forName) {
      this.$refs[forName].validate(async () => {
        //向后端要数据
        const {data: res} = await this.$http.post("http://localhost:9000/user/login", this.ruleForm);
        if (res.userId > 0) {
          sessionStorage.setItem("token", res.userId);
          sessionStorage.setItem("username", res.username);
          this.$cookies.set("token", res.userId);
          this.$cookies.set("username", res.username);
          this.$router.push("/universal");
          this.$notify({
            title: '成功',
            message: '登录成功',
            type: 'success'
          });
        } else {
          this.$notify.error({
            title: '失败',
            message: '用户或密码不存在'
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    registered() {
      this.$router.push("/registered");
    },
    retrieve() {
      this.$router.push("/retrieve");
    }
  }
}
</script>

<style lang="less" scoped>

// 登录框
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px; // 圆角
  position: absolute; // 绝对定位
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%); // 根据自己位置 以自己为长度位移
  // 头像框
  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%; // 加圆角
    padding: 10px;
    box-shadow: 0 0 10px #ddd; // 盒子阴影
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #0ee;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%; // 加圆角
      background-color: #eee;
    }
  }

  .btns {
    display: flex; // 弹性布局
    justify-content: flex-end; // 尾部对齐
  }

  .demo-ruleForm {
    position: absolute;
    bottom: 0%;
    padding: 0 10px;
    box-sizing: border-box; // 设置边框
    width: 90%;
  }
}
</style>

